package com.ruoyi.index.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.index.domain.FhIndexPostinfo;
import com.ruoyi.index.mapper.FhIndexPostinfoMapper;
import com.ruoyi.index.service.IFhIndexPostinfoService;
import com.ruoyi.map.domain.ScreeningCatchDto;
import com.ruoyi.map.domain.TimeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 发布信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-12
 */
@Service
public class FhIndexPostinfoServiceImpl implements IFhIndexPostinfoService 
{
    @Autowired
    private FhIndexPostinfoMapper fhIndexPostinfoMapper;

    /**
     * 查询发布信息
     * 
     * @param id 发布信息主键
     * @return 发布信息
     */
    @Override
    public FhIndexPostinfo selectFhIndexPostinfoById(Long id)
    {
        return fhIndexPostinfoMapper.selectFhIndexPostinfoById(id);
    }

    /**
     * 查询发布信息列表
     * 
     * @param fhIndexPostinfo 发布信息
     * @return 发布信息
     */
    @Override
    public List<FhIndexPostinfo> selectFhIndexPostinfoList(FhIndexPostinfo fhIndexPostinfo)
    {
        return fhIndexPostinfoMapper.selectFhIndexPostinfoList(fhIndexPostinfo);
    }

    /**
     * 新增发布信息
     * 
     * @param fhIndexPostinfo 发布信息
     * @return 结果
     */
    @Override
    public FhIndexPostinfo insertFhIndexPostinfo(FhIndexPostinfo fhIndexPostinfo)
    {
        fhIndexPostinfoMapper.insertFhIndexPostinfo(fhIndexPostinfo);
        return fhIndexPostinfo;
    }

    /**
     * 修改发布信息
     * 
     * @param fhIndexPostinfo 发布信息
     * @return 结果
     */
    @Override
    public int updateFhIndexPostinfo(FhIndexPostinfo fhIndexPostinfo)
    {
        fhIndexPostinfo.setUpdateTime(DateUtils.getNowDate());
        return fhIndexPostinfoMapper.updateFhIndexPostinfo(fhIndexPostinfo);
    }

    /**
     * 批量删除发布信息
     * 
     * @param ids 需要删除的发布信息主键
     * @return 结果
     */
    @Override
    public int deleteFhIndexPostinfoByIds(Long[] ids)
    {
        return fhIndexPostinfoMapper.deleteFhIndexPostinfoByIds(ids);
    }

    /**
     * 删除发布信息信息
     * 
     * @param id 发布信息主键
     * @return 结果
     */
    @Override
    public int deleteFhIndexPostinfoById(Long id)
    {
        return fhIndexPostinfoMapper.deleteFhIndexPostinfoById(id);
    }


    //根据好友id数组，以及时间倒序查询捕获表
    @Override
    public List<FhIndexPostinfo> selectFhIndexPostinfoListByIds(Long[] friends) {
        return fhIndexPostinfoMapper.selectFhIndexPostinfoListByIds(friends);
    }


    //根据天数，月份，鱼类id筛选渔获信息
    @Override
    public List<FhIndexPostinfo> screeningCatchList(ScreeningCatchDto screeningCatchDto) {
        List<FhIndexPostinfo> fhIndexPostinfoVo = new ArrayList<>();

        //先根据时间筛选出捕获信息列表，然后再筛选鱼类id;
        int day = screeningCatchDto.getDay().intValue();
        int month = screeningCatchDto.getMonth().intValue();
        Long fishId = screeningCatchDto.getFishId();
        String fishid = String.valueOf(fishId);
        LocalDate now = LocalDate.now();
        LocalDate localDate = LocalDate.of(now.getYear(), month, 15);
        //日期上限
        LocalDate endTime= localDate.plusDays(day);
        //日期下限
        LocalDate startTime = localDate.plusDays(-day);
        TimeDto timeDto = new TimeDto(startTime, endTime);
        List<FhIndexPostinfo> postinfoList= fhIndexPostinfoMapper.screeningCatchListByDate(timeDto);
        for (FhIndexPostinfo fhIndexPostinfo : postinfoList) {
            String[] fishIdList = fhIndexPostinfo.getFishIds().split(",");
            for (String s : fishIdList) {
                if (fishid.equals(s)) {
                    fhIndexPostinfoVo.add(fhIndexPostinfo);
                    break;
                }
            }
        }
        return fhIndexPostinfoVo;
    }

}
